<?php
namespace app\controllers;

use app\logic\CommonLogic;
use app\logic\GoodsLogic;
use app\logic\ShopLogic;
use app\models\Area;
use Yii;


class IndexController extends BaseController
{

    const LIMIT = 10;
	public function actionIndex()
	{
        $this->response(time());
	}

    /**
     * 标题栏主题色信息
     * @apiDescription   by Jim
     * @api              {post} /index/get-info 标题栏主题色信息
     * @apiName          get-info
     * @apiVersion       1.0.0
     * @apiGroup         Index
     * @apiParam {int} mall_id 商城id
     * @apiSampleRequest /index/get-info?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
            "mall_color":"FFFFFF",                //色值
            "hide_title":0                      //是否隐藏标题栏，0为不隐藏，1为隐藏
        }
     * @apiError         200000
     */
    public function actionGetInfo()
    {
        $mall_id = $this->getQueryparams('mall_id',0);
        $commonLogic = new CommonLogic();
        $mall = $commonLogic->getMall($mall_id);
        if($mall['hide_title'])
            $hide_title = 1;
        else
            $hide_title = 0;
        $this->response(['mall_name'=>$mall['mall_name'],'mall_color'=>$mall['mall_color'],'hide_title'=>$hide_title]);
    }

    /**
     * 搜索
     * @apiDescription   by Jim
     * @api              {post} /index/search 搜索
     * @apiName          search
     * @apiVersion       1.0.0
     * @apiParam {string} keyword 搜索关键词
     * @apiParam {int} orderBy 排序 总共有四种， 1 综合 2 销量高到低  3 价格低到高  4 价格高到低，默认是综合
     * @apiParam {int} category_id 分类ID，传空或传0为全部分类
     * @apiParam {int} page 页码，默认为1
     * @apiGroup         Index
     * @apiSampleRequest /index/search?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
        "goods_id": 12, // int 商品Id
        "goods_pic": "https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3776766248,514215818&fm=173&app=25&f=JPEG?w=550&h=367&s=B9248F180C1448CE1E9870C00300F0A9", // 商品图片
        "goods_name": "商品标题商品标题商品标题", // 商品标题
        "goods_price": 12.00 // float 商品价格
      }]
     * @apiError         10003    获取失败
     */
    public function actionSearch(){
        $keyword = $this->getQueryparams('keyword',"");
        $orderBy = $this->getQueryparams('orderBy',1);
        $category_id = $this->getQueryparams('category_id',0);
        $page = $this->getQueryparams('page',1);
        $goodsLogic = new GoodsLogic();
        $result = $goodsLogic->search($keyword,$orderBy,$category_id,$page,self::LIMIT);
        foreach($result AS &$item){
            $item['goods_pic'] = Yii::$app->params['pic_domain'].$item['goods_pic'];
        }
        $this->response($result);
    }

    /**
     * 首页banner
     * @apiDescription   by Jim
     * @api              {post} /index/banner 首页banner
     * @apiName          banner
     * @apiVersion       1.0.0
     * @apiGroup         Index
     * @apiParam {int} mall_id 商城id
     * @apiSampleRequest /index/banner?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
        "adv_link": "http://xxxx", // 广告对应的链接地址
        "adv_pic": "xxx", // 广告对应的图片地址
     }
     * @apiError         200001
     */
    public function actionBanner()
    {
        $mall_id = $this->getQueryparams('mall_id',0);
        $commonLogic = new CommonLogic();
        $mall = $commonLogic->getMall($mall_id);
        $this->response(['adv_pic'=>Yii::$app->params['pic_domain'].$mall['adv_pic'],'adv_link'=>$mall['adv_link']]);
    }

    /**
     * 分类
     * @apiDescription   by Jim
     * @api              {post} /index/category 分类
     * @apiName          category
     * @apiVersion       1.0.0
     * @apiParam {int} mall_id 商城id
     * @apiGroup         Index
     * @apiSampleRequest /index/category?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
        "category_id": 1, // 分类ID
        "category_name": "分类名" // 分类名
    }]
     * @apiError         200002    获取失败
     */
    public function actionCategory(){
        $mall_id = $this->getQueryparams('mall_id',0);
        $shopLogic = new ShopLogic();
        $category = $shopLogic->getCategory($mall_id);
        $this->response($category);
    }
    /**
     * 商品列表
     * @apiDescription   by Jim
     * @api              {post} /index/goods-list 商品列表
     * @apiName          goods-list
     * @apiVersion       1.0.0
     * @apiParam {int} mall_id 商城id
     * @apiParam {int} category_id 分类ID，传空或传0为全部分类
     * @apiParam {int} page 页码，默认为1
     * @apiParam {int} limit 每页数量，默认为10
     * @apiGroup         Goods
     * @apiSampleRequest /index/goods-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
        "mall_style": 0, // 显示方式，0列表，1一行2列
        "list": [{
        "goods_id": 1, // 商品ID
        "goods_name": "fdsafsafsa", // 商品名
        "goods_listpic": "http://ffddd", // 商品图片， 带http
        "goods_summary": "dsafasfsa", // 商品描述
        "market_price": 128.00, // 原价
        "real_price": 100.00 // 折扣价
        }]
     * }
     * @apiError         30000    获取失败
     */
    public function actionGoodsList(){
        $mall_id = $this->getQueryparams('mall_id',0);
        $category_id = $this->getQueryparams('category_id',0);
        $page = $this->getQueryparams('page',1);
        $limit = $this->getQueryparams('limit',10);
        $commonLogic = new CommonLogic();
        $mall = $commonLogic->getMall($mall_id);
        $shopLogic = new ShopLogic();
        $goods = $shopLogic->getGoodsList($mall_id,$category_id,$page,$limit);
        $this->response(['mall_style'=>$mall['mall_style'],'list'=>$goods]);
    }



    public function actionGetArea(){
        $area = Area::find()->where(['region_level'=>3])->asArray()->all();
        $result1 = [];
        foreach($area AS $key=>$value){
            $result1[$value['parent_code']]['children'][] = ['region_id'=>$value['region_code'],'region_name'=>$value['region_name']];
        }
        $result2 = [];
        $area = Area::find()->where(['region_level'=>2])->asArray()->all();
        foreach($area AS $key=>$value){
            $result1[$value['region_code']]['region_name'] = $value['region_name'];
            $result1[$value['region_code']]['region_id'] = $value['region_code'];
            $result2[$value['parent_code']]['children'][] = $result1[$value['region_code']];
        }
        $area = Area::find()->where(['region_level'=>1])->asArray()->all();
        foreach($area AS $key=>$value){
            $result2[$value['region_code']]['region_name'] = $value['region_name'];
            $result2[$value['region_code']]['region_id'] = $value['region_code'];
        }
        $result = (array_values($result2));
        $this->response($result);
    }

    public function actionGetRegion(){
        $area = Area::find()->where(['region_level'=>1])->asArray()->all();
        foreach($area AS $key=>$value){
            $res[100000][$value['region_code']] = $value['region_name'];
        }
        $area = Area::find()->where(['region_level'=>2])->asArray()->all();
        foreach($area AS $key=>$value){
            $res[$value['parent_code']][$value['region_code']] = $value['region_name'];
        }
        $area = Area::find()->where(['region_level'=>3])->asArray()->all();
        foreach($area AS $key=>$value){
            $res[$value['parent_code']][$value['region_code']] = $value['region_name'];
        }
        echo json_encode($res);
    }

}